package com.logic.saas.util.interceptor;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsConstants;

import com.logic.saas.action.LoginAction;
import com.logic.saas.enums.ConstantEnum;
import com.logic.saas.enums.SessionKey;
import com.logic.saas.pojo.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("unchecked")
public class AuthenticationInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = -1324347222323786414L;

	private String defaultEncoding;
	Map<String, String> noValidateMethods = new HashMap<String, String>(){ 
		{ put("com.logic.saas.action.CommonAction.agreeRoleForward", "agreeRoleForward");
		put("com.logic.saas.action.user.UserAction.execute", "execute");
		put("com.logic.saas.action.user.UserAction.register", "register");
		} 
	};
	
	
    @Inject(StrutsConstants.STRUTS_I18N_ENCODING)
    public void setDefaultEncoding(String val) {
    	if(StringUtils.isNotBlank(val)){
    		defaultEncoding = val;
    	}
    }
    
    public String getDefaultEncoding(){
    	if(this.defaultEncoding == null){
    		this.defaultEncoding = "UTF-8";
    	}
    	
    	return this.defaultEncoding;
    }

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		String result = Action.LOGIN;
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding(this.getDefaultEncoding());
		Object action = invocation.getAction();
		
		String actionName = action.getClass().getName();
		String methodName = invocation.getProxy().getMethod();
		
		if (action instanceof LoginAction) {
			return invocation.invoke();
		}
		
		if(noValidateMethods.containsKey(actionName+"."+methodName)){
			String methodNameP = noValidateMethods.get(actionName+"."+methodName);
			if(methodName.equals(methodNameP)){
				return invocation.invoke();
			}
		 }
		
		User user = (User)session.get(SessionKey.USER);
		if (user != null && user.getId() != null) {
			result = invocation.invoke();
		}else{
			// 获取HttpServletRequest对象
            HttpServletRequest req = ServletActionContext.getRequest();
            // 获取此请求的地址，请求地址包含application name，进行subString操作，去除application name
            String path = req.getRequestURI().substring(10);
            // 获得请求中的参数
            String queryString = req.getQueryString() == null? "" : req.getQueryString();
            // 拼凑得到登陆之前的地址
            String realPath = path + "?" + queryString;
            // 存入session，方便调用
            session.put(ConstantEnum.PRE_LOGIN_PAGE.getValue(), realPath);
			
		}
		
		return result;
		
		
	}
	
}
